﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web.Security;

public partial class Dossier_Default : System.Web.UI.Page
{
    private SqlConnection con;

    protected void Page_Load(object sender, EventArgs e)
    {
        // redirect users in user role VerplegerA 
        if (User.IsInRole("VerplegerA"))
        {
            Response.Redirect("~/Login.aspx");
        }

        if (Request.QueryString["PID"] == null || Request.QueryString["PID"] == "")
        {
            Melding.Text = "Geen patient opgegeven";
            Button1.Visible = false;
            return;
        }
        Button1.Visible = true;
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        SqlCommand hTests = new SqlCommand("SELECT *, LEN(Naam) AS lengte FROM Labtest ORDER BY LEN(Naam), Naam", con);
        SqlDataReader rTests = hTests.ExecuteReader();
        int i=0;
        int div=8;
        TableRow tRow = new TableRow();
        while (rTests.Read())
        {
            i++;
            Dossier_TestAanvraag t = (Dossier_TestAanvraag) Page.LoadControl("~/Dossier/TestAanvraag.ascx");
            t.Labid = rTests.GetInt32(0);
            t.Naam = rTests.GetString(1);
            TableCell tCell = new TableCell();
            tCell.Controls.Add(t);
            tCell.ColumnSpan = 40 / div;
            tCell.Wrap = false;
            tRow.Cells.Add(tCell);
            if (i % div == 0)
            {
                Tests.Rows.Add(tRow);
                tRow = new TableRow();
                if (div == 8 && rTests.GetInt32(2) > 5)
                {
                    div = 5;
                    i = 0;
                }
                else if (div == 5 && rTests.GetInt32(2) > 11)
                {
                    div = 2;
                    i = 0;
                }
            }
        }
        rTests.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // membership object
        MembershipUser myObject = Membership.GetUser();
        string userName = myObject.UserName;

        int[] aTests = getChecked();
        if (aTests.Length == 0)
        {
            Melding.Text = "Geen labtests geselecteerd";
            return;
        }
        SqlCommand cmd = new SqlCommand(
                "INSERT INTO Labaanvraag(aanvrager, patient, datum, status) " +
                "VALUES( (SELECT MDWID FROM Medewerker WHERE UserName = '"+userName+"'), " + Request.QueryString["PID"] +
                ", '" + DateTime.Today.ToString("dd-MM-yyyy") + "', 0)", con);
        cmd.ExecuteNonQuery();
        cmd.CommandText = "SELECT @@IDENTITY";
        int LABAVID = (int)(decimal)cmd.ExecuteScalar();
        for (int i = 0; i < aTests.Length; i++)
        {
            cmd.CommandText = "INSERT INTO Testaanvraag(labid, labavid) " +
                                "VALUES(" + aTests[i] + ", " + LABAVID + ")";
            cmd.ExecuteNonQuery();
        }
        Melding.Text = "Labaanvraag succesvol geplaatst";
    }

    private int[] getChecked()
    {
        List<int> aAangevraagd = new List<int>();
        IEnumerator rs = Tests.Rows.GetEnumerator();
        while (rs.MoveNext())
        {
            TableRow r = (TableRow) rs.Current;
            IEnumerator cs = r.Cells.GetEnumerator();
            while (cs.MoveNext())
            {
                TableCell c = (TableCell)cs.Current;
                IEnumerator it = c.Controls.GetEnumerator();
                while (it.MoveNext())
                {
                    Object o = it.Current;
                    if (!(o is Dossier_TestAanvraag))
                        continue;
                    Dossier_TestAanvraag t = (Dossier_TestAanvraag)o;
                    if (t.Checked)
                        aAangevraagd.Add(t.Labid);
                }
            }
        }
        return aAangevraagd.ToArray();
    }
}